<?php

namespace app\admin\controller;

use think\Db;
use think\Validate;
class Team extends Base{
	public function index(){
		return $this->fetch();
	}
	public function index_data(){
		$input = input();
		$where=[];
		if (isset($input['keywords']) and !empty($input['keywords'])) {
			$where[] = ['u.username|t.nickname','like', '%' . $input['keywords'] . '%'];
		}
		if (isset($input['seach_time']) and !empty($input['seach_time'])) {
			$str = explode('~',$input['seach_time']);
		    $star_time=$str[0]? $str[0] : '';
		    $end_time=$str[1] ? $str[1] : '';
			$where[]=['t.add_time','between',[strtotime($star_time),strtotime($end_time)+86400]];
		}
		$page       = ($input['page']-1)*$input['limit'];
		$limit      = $input['limit'];
		$data = Db::name('team')->alias('t')
		->join('user u','t.t_id=u.id','LEFT')
		->field('t.*,u.username t_name')
		->where('t.is_del',0)
		->where($where)
		->limit($page,$limit)
		->select();
		
		$count = Db::name('team')->alias('t')
		->join('user u','t.t_id=u.id','LEFT')
		->field('t.*,u.username t_name')
		->where('t.is_del',0)
		->where($where)
		->count();
		
		return hSuccess('OK', $data,$count,ceil($count/$limit));
	}
	
	public function add(){
		if($this->request->isPost()) {
			//是提交操作
			$post = $this->request->post();
			$rule = [
			    'nickname'   => 'require',
			    't_id' => 'require',
			    'photo' => 'require',
			];
			$msg = [
			    'nickname.require'  => '团昵称不能为空',
			    't_id.require'  	=> '请选择团长',
			    'photo.require'  	=> '请上传团头像',
			];
			
			$data = [
				'head_img'		=>	$post['photo'],
				'nickname'	=>	$post['nickname'],
				't_id'	=>	$post['t_id'],
				'status'	=>	isset($post['status'])?$post['status']:0,
				'add_time'	=>	time(),
			];
			
			$validate   = Validate::make($rule,$msg);
		    //验证部分数据合法性
		    if (!$validate->check($post)) {
		        $this->error('提交失败：' . $validate->getError());
		    }
			
			$t_id = Db::name('team')->insertGetId($data);
			
			$res = Db::name('user')->where('id',$post['t_id'])->update(['team_id'=>$t_id,'update_time'=>time()]);
		    if($t_id>0 && $res) {
		    	return hSuccess('操作成功');
		    } else {
				return hError(302,'操作失败');
		    }
		}
		$user = Db::name('user')->where('identity',2)->where('team_id',0)->order('id asc')->field('id,username')->select();
		
		$this->assign('user',$user);
		return $this->fetch();
	}
	public function edit(){
		// 获取参数
		$post = $this->request->param();
		// 提交操作
		if($this->request->isAjax()){
			$rule = [
			    'nickname'   => 'require',
			    'photo' => 'require',
			];
			$msg = [
			    'nickname.require'  => '团昵称不能为空',
			    'photo.require'  	=> '请上传团头像',
			];
			
			$data = [
				'head_img'		=>	$post['photo'],
				'nickname'	=>	$post['nickname'],
				'status'	=>	isset($post['status'])?$post['status']:0,
				'add_time'	=>	time(),
			];
			
			$validate   = Validate::make($rule,$msg);
		    //验证部分数据合法性
		    if (!$validate->check($post)) {
		        $this->error('提交失败：' . $validate->getError());
		    }
			
			
			$res = Db::name('team')->where('id',$post['id'])->update($data);
	        if($res){
	            return hSuccess('操作成功');
	        }else{
	            return hError('-1','操作失败');
	        }
		}
		// 展示界面
		$info = Db::name('team')->find($post['id']);
		$this->assign('info',$info);
		$user = Db::name('user')->where('identity',2)->where('id',$info['t_id'])->order('id asc')->field('id,username')->find();
		$this->assign('user',$user);
	    return $this->fetch();
	}
	public function delete(){
		$input = $this->request->param();
		if($this->request->isAjax()) {
			// 解散团队
			
			// 1、修改所有团员的状态
			
			$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
			$res1 = Db::name('team')->where('id',$id)->update(['is_del'=>1,'update_time'=>time()]);
			$res2 = Db::name('user')->where('team_id',$id)->update(['team_id'=>0,'update_time'=>time()]);
			if($res1 && $res2) {
				return hSuccess('操作成功');
			} else {
				return hError(410,'操作失败');
			}
		}
	}
	
	// 添加成员
	public function add_member(){
		if($this->request->isAjax()) {
			$data = input();
			$item_ids = $data['u_id'];
			
			foreach($item_ids as $k=>$v){
				$res[] = Db::name('user')->where('id',$v)->update(['team_id'=>$data['t_id'],'update_time'=>time()]);
			}
			if($res) {
				return hSuccess('操作成功');
			} else {
				return hError(410,'操作失败');
			}
		}
		
		$user = Db::name('user')->where('identity',2)->where('team_id',0)->order('id asc')->field('id,username')->select();
		$this->assign('user',$user);
		
		$this->assign('team_id',$this->request->param('id'));
		return $this->fetch();
	}
	
	// 成员列表
	public function member(){
		$data = input();
		$info = Db::name('team')->alias('t')
		->join('user u','t.t_id=u.id','LEFT')
		->where('t.id',$data['id'])
		->field('t.*,u.username u_name')
		->find();
		$info['num'] = Db::name('user')->where('team_id',$data['id'])->count();
		$this->assign('info',$info);
		
		return $this->fetch();
	}
	// 成员列表
	public function member_data(){
		$input = input();
		$where=[];
		
		$page       = ($input['page']-1)*$input['limit'];
		$limit      = $input['limit'];
		
		$data = Db::name('user')->alias('u')
		->join('staff_type s','u.staff_type=s.id','LEFT')
		->where('u.team_id',$input['team_id'])
		->field('u.id,u.username,s.name s_name,u.add_time')
		->limit($page,$limit)
		->select();
		
		$count = Db::name('user')
		->where('team_id',$input['team_id'])
		->count();
		
		return hSuccess('OK', $data,$count,ceil($count/$limit));
	}
	// 踢出单个成员
	public function kick(){
		$input = $this->request->param();
		if($this->request->isAjax()) {
			// 解散团队
			
			// 1、修改所有团员的状态
			
			$id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
			$res = Db::name('user')->where('id',$id)->update(['team_id'=>0,'update_time'=>time()]);
			if($res) {
				return hSuccess('操作成功');
			} else {
				return hError(410,'操作失败');
			}
		}
	}
	// 切换团长
	public function change(){
		if($this->request->isAjax()) {
			$data = input();
			$res1 = Db::name('team')->where('id',$data['t_id'])->update(['t_id'=>$data['u_id'],'update_time'=>time()]);
			$res2 = Db::name('user')->where('id',$data['u_id'])->update(['team_id'=>$data['t_id'],'update_time'=>time()]);
			if($res1 && $res2 ) {
				return hSuccess('操作成功');
			} else {
				return hError(410,'操作失败');
			}
		}
		$team_id = $this->request->param('id');
		$user = Db::name('user')->where('identity',2)->where('id','<>',input('old_tz'))->where('team_id',$team_id)->order('id asc')->field('id,username')->select();
		$this->assign('user',$user);
		$this->assign('team_id',$team_id);
		
		return $this->fetch();
	}
}
?>